package com.ld.security.server.admin.modules.ge.vo;

import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.core.base.column.ListJson;
import com.ld.security.common.core.util.JsonUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.anno.QueryKey;
import com.ld.security.common.mybatis.anno.RemoveKey;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.constant.SqlCondition;

import com.ld.security.server.admin.modules.ge.entity.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@ApiModel("视图详情")
public class RenderViewVo implements SuperVo,ProxyVo {

    private String id;


    private Map viewArgs;

    @ApiModelProperty("视图")
    @QueryKey
    @RemoveKey
    @SqlKey(name = "id",valueField = "id")
    private GeViewEntity view;

    @QueryKey
    @SqlKey(name = "id",valueField = "view.packId")
    private GePackEntity pack;

    @SqlKey(name = "id",valueField = "view.projectId")
    @QueryKey
    @ApiModelProperty("工程")

    private GeProjectEntity project;

    @SqlKey(name = "id",valueField = "view.viewTelId")
    @QueryKey
    @ApiModelProperty("模型模板")

    private GeViewTelEntity viewTel;


    @ApiModelProperty("模型")
    @QueryKey
    @SqlKey(name = "viewId",valueField = "view.id")
    @RemoveKey
    private List<GeModelEntity> models;


    @QueryKey(groupKey = "modelId")
    @ApiModelProperty("模型字段")
    @SqlKey(name = "modelId",valueField = "models.id",value = SqlCondition.in)
    @RemoveKey
    private Map<String,List<GeFieldEntity>> fieldMap;

    @RemoveKey
    @ApiModelProperty("链接字段")
    @QueryKey(groupKey = "modelId")
    @SqlKey(name = "modelId",valueField = "models.id")
    private Map<String,List<GeLinkEntity>> linkMap;

    @ApiModelProperty("组件对象")
    @QueryKey(groupKey = "id")
    @SqlKey(name = "id",valueField = "fieldMap.compId",value = SqlCondition.in)
    private Map<String,GeCompEntity> compMap;



    @ApiModelProperty("可视化模型")
    @QueryKey(groupKey = "modelId")
    @SqlKey(name = "modelId",value = SqlCondition.in,valueField = "models.id")
    private Map<String,List<GeChartEntity>> chartMap;

    private List<ModelVo> modelList;


    @ApiModelProperty("函数map")
    @QueryKey(groupKey = "id")
    @SqlKey(name = "viewId",valueField = "id",value = SqlCondition.eq)
    private Map<String,GeFuncEntity> funcMap;

    /**
     * 预警模型
     */
    private List<EarlyDetailsVo.EarlyDetails> earlyModels;

    private static Map defMap = new HashMap();

    static GeCompEntity defComp ;

    static {
        defComp = new GeCompEntity();
        defComp.setCode("anon");
        defComp.setConfig(new ListJson());
    }

    @Override
    public void queryAfter() {

        if(fieldMap == null){
            fieldMap = defMap;
        }
        if(linkMap == null){
            linkMap = defMap;
        }
        if(compMap == null){
            compMap = defMap;
        }


        this.modelList = new LinkedList<>();

        for (GeModelEntity model : models) {
            List<GeFieldEntity> vFields =  fieldMap.get(model.getId());
            List<GeLinkEntity> linkEntities =  linkMap.get(model.getId());
            List<GeChartEntity> chartEntities =  chartMap.get(model.getId());

            if(vFields == null){
                vFields = new LinkedList<>();
            }
            if(linkEntities == null){
                linkEntities = new LinkedList<>();
            }
            if(chartEntities == null){
                chartEntities = new LinkedList<>();
            }

            ModelVo vo = JsonUtil.clone(model,ModelVo.class);

          for (GeChartEntity chartEntity : chartEntities) {
              Map map =  chartEntity.getConfig().toMap();
              chartEntity.setConfigMap(map);
          }
            vo.setCharts(chartEntities);
           List<GeFieldCompVo> comps =   vFields.stream().sorted((x,y) -> x.getSortVal() - y.getSortVal()).map(v -> {
               GeFieldCompVo comp = JsonUtil.clone(v,GeFieldCompVo.class);

               GeCompEntity geColumnEntity =  this.compMap.get(v.getCompId());
             if(geColumnEntity == null){
                 geColumnEntity = defComp;
             }
             if(StringUtil.isNotNull(v.getClickFuncId())){
                 GeFuncEntity func =  funcMap.get(v.getClickFuncId());
                 comp.setClickEntity(func);
             }

               if(StringUtil.isNotNull(v.getShowFuncId())){
                   GeFuncEntity func =  funcMap.get(v.getShowFuncId());
                   comp.setShowEntity(func);
               }

               geColumnEntity.setConfigMap(geColumnEntity.getConfig().toMap());

              comp.setComp(geColumnEntity);
                return comp;
            }).collect(Collectors.toList());

            vo.setLinks(linkEntities);
            vo.setFields(comps);
            modelList.add(vo);



        }



        this.viewArgs=this.view.getViewArgs().toMap();


    }


    /**
     * 模型带字段
     */
    @Data
    public static class ModelVo extends  GeModelEntity{

        //字段

        private List<GeFieldCompVo> fields;
        //连接

        private List<GeLinkEntity> links;

        private List<GeChartEntity> charts;


    }

    /**
     * 字段待组件对象
     */
    @Data
    public static class GeFieldCompVo extends GeFieldEntity {

        private GeCompEntity comp;

        private GeFuncEntity clickEntity;

        private GeFuncEntity showEntity;


    }

}
